package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.exception.CustException;
import com.heima.model.threadLocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private ApBehaviorEntryServiceImpl apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        Map<String, Object> result = new HashMap<>();
        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            //2. 未登录 直接将 4种行为 均返回false
            result.put("isfollow", false);
            result.put("islike", false);
            result.put("isunlike", false);
            result.put("iscollection", false);
            return ResponseResult.okResult(result);
        }
        //3. 如果登录
        //4. 查询行为实体
        Integer id = user.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(id, null);
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体对象不存在或者创建失败!");
        }
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        //5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if ((short) 0 == apLikesBehavior.getOperation()) {
            result.put("islike", true);
        } else {
            result.put("islike", false);
        }
        //6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if ((short) 0 == apUnlikesBehavior.getType()) {
            result.put("isunlike", true);
        } else {
            result.put("isunlike", false);
        }
        //7. 根据行为实体 文章ID查询 是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if (apCollection != null) {
            result.put("iscollection", true);
        } else {
            result.put("iscollection", false);
        }
        //8. 根据登录用户id 去redis中查询是否关注该作者
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        Double score = zSetOperations.score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
        if (score != null) {
            result.put("isfollow", true);
        }else {
            result.put("isfollow", false);
        }
        //9. 封装结果 返回
        return ResponseResult.okResult(result);
    }
}
